import request from '../utils/request';

/**
 * 认证相关API
 */
const authApi = {
  /**
   * 用户登录
   * @param {Object} credentials - 登录凭证
   * @param {string} credentials.username - 用户名
   * @param {string} credentials.password - 密码
   * @returns {Promise} - 登录结果
   */
  login: async (credentials) => {
    try {
      const response = await request.post('/auth/login', credentials);
      
      // 保存token到本地存储
      if (response.code === 200 && response.data && response.data.token) {
        localStorage.setItem('adminToken', response.data.token);
      }
      
      return response;
    } catch (error) {
      console.error('登录失败:', error);
      throw error;
    }
  },
  
  /**
   * 用户登出
   * @returns {Promise} - 登出结果
   * @returns {Promise} - 登出结果
   */
  logout: async () => {
    try {
      const response = await request.post('/admin/auth/logout');
      localStorage.removeItem('adminToken');
      return response;
    } catch (error) {
      console.error('登出失败:', error);
      localStorage.removeItem('adminToken');
      throw error;
    }
  },
  

};

export default authApi;
export { authApi };
